﻿using System;
using System.Configuration;
using System.Web.Mvc;
using IntellectShop.Common;
using IntellectShop.Web2.ViewModels;

namespace IntellectShop.Web2.Controllers
{
	[IntellectShop.Web2.Code.NoCache]
	[IntellectShop.Web2.Code.HandleErrorWithELMAH]
	[ValidateInputAttribute(false)]
	public class FeedbackController : Controller
	{
		//
		// GET: /Feedback/
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Index()
		{
			var feedback = Common.Feedback.RetrieveAll();
			return View(feedback);
		}

		//
		// GET: /Feedback/Details/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Details(int feedbackID)
		{
			var feedback = Common.Feedback.RetrieveByFeedbackID(feedbackID);
			return View(feedback);
		}

		//
		// GET: /Feedback/Create
		public ActionResult Create()
		{
			var viewModel = new FeedbackEditViewModel { Feedback = new Common.Feedback() }; 
			return View(viewModel);
		}

		//
		// POST: /Feedback/Create
		[HttpPost]
		public ActionResult Create(Common.Feedback feedback)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Feedback!");

				feedback.FeedbackOwnerID = Common.Globals.GetOwnerID(this.HttpContext);
				feedback.Create(this.HttpContext.User.Identity.Name);
				this.SendNotificationMail(Common.Feedback.RetrieveByFeedbackID(feedback.FeedbackID));
				return RedirectToAction("AboutCompany", "Home", null);
			}
			catch (Exception e)
			{
				var viewModel = new FeedbackEditViewModel {
					Feedback = new Common.Feedback(),
					Message = e.Message };
				return View(viewModel);
			}
		}

		private void SendNotificationMail(Common.Feedback feedback)
		{
			string storeEmail = ConfigurationManager.AppSettings["StoreEmail"];
			string managerEmail = ConfigurationManager.AppSettings["ManagerEmail"];
			string forceUsePickupDirectory = ConfigurationManager.AppSettings["ForceUsePickupDirectory"];
			if (!String.IsNullOrEmpty(forceUsePickupDirectory))
			{
				forceUsePickupDirectory = Server.MapPath(forceUsePickupDirectory);
			}

			MailUtils.Send(
				storeEmail,
				managerEmail,
				Server.MapPath("~/App_Data/MailTemplates/feedbackTemplate.htm"),
				"windows-1251",
				feedback.Print(),
				feedback.FeedbackOwnerID,
				forceUsePickupDirectory,
				null);
		}

		//
		// GET: /Feedback/Edit/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Edit(int feedbackID)
		{
			var viewModel = new FeedbackEditViewModel
			{
				Feedback = Common.Feedback.RetrieveByFeedbackID(feedbackID),
			};
			return View(viewModel);
		}

		//
		// POST: /Feedback/Edit/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Edit(int feedbackID, FormCollection collection)
		{
			var feedback = Common.Feedback.RetrieveByFeedbackID(feedbackID);
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Feedback!");

				UpdateModel(feedback, "Feedback");
				feedback.Update(this.HttpContext.User.Identity.Name);
				return Redirect(Url.RouteUrl(new { action = "Index" }) + "#id" + feedbackID);
			}
			catch (Exception e)
			{
				var viewModel = new FeedbackEditViewModel
				{
					Feedback =  Common.Feedback.RetrieveByFeedbackID(feedbackID),
					Message = e.Message
				};
				return View(viewModel);
			}
		}

		//
		// GET: /Feedback/Delete/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Delete(int feedbackID)
		{
			var viewModel = new FeedbackRemoveViewModel { Feedback = Common.Feedback.RetrieveByFeedbackID(feedbackID) };
			return View(viewModel);
		}

		//
		// POST: /Feedback/Delete/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Delete(int feedbackID, FormCollection collection)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Feedback!");

				Common.Feedback.Delete(feedbackID);
				return RedirectToAction("Index");
			}
			catch (Exception e)
			{
				var viewModel = new FeedbackRemoveViewModel
				{
					Feedback = Common.Feedback.RetrieveByFeedbackID(feedbackID),
					Message = e.Message
				};
				return View(viewModel);
			}
		}
	}
}
